<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>路由变更记录 - 管理界面</title>
    <style>
        .container {
            max-width: 1200px;
            margin: 0 auto;
            padding: 2rem;
        }

        .section-header {
            margin-bottom: 2rem;
        }

        .table {
            width: 100%;
            border-collapse: collapse;
            margin-top: 1rem;
        }

        .table th,
        .table td {
            padding: 0.75rem;
            border: 1px solid #dee2e6;
        }

        .table th {
            background-color: #f8f9fa;
            font-weight: 600;
        }

        .alert {
            padding: 1rem;
            margin-bottom: 1rem;
            border-radius: 4px;
        }

        .alert-danger {
            background-color: #f8d7da;
            color: #721c24;
            border: 1px solid #f5c6cb;
        }

        .alert-info {
            background-color: #d1ecf1;
            color: #0c5460;
            border: 1px solid #bee5eb;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="section-header">
            <h1>路由变更记录</h1>
        </div>
        
        <div id="changeList"></div>
    </div>

    <script>
        // 获取变更记录
        async function fetchChangeList() {
            try {
                const response = await fetch('/api/routes/changes', {
                    method: 'GET',
                    headers: {
                        'Content-Type': 'application/json',
                        'X-Requested-With': 'XMLHttpRequest'
                    },
                    credentials: 'include'
                });
                
                if (!response.ok) {
                    throw new Error(`HTTP error! status: ${response.status}`);
                }
                
                const data = await response.json();
                if (data.status === 'success') {
                    displayChangeList(data.data);
                } else {
                    showError(data.message || '获取路由变更列表失败');
                }
            } catch (error) {
                console.error('获取路由变更列表失败:', error);
                showError('获取路由变更列表失败，请检查网络连接');
            }
        }

        // 显示变更记录
        function displayChangeList(changes) {
            const changeList = document.getElementById('changeList');
            if (!changes || changes.length === 0) {
                changeList.innerHTML = '<div class="alert alert-info">当前无主机更改数据</div>';
                return;
            }

            const table = document.createElement('table');
            table.className = 'table';
            
            // 表头
            const thead = document.createElement('thead');
            thead.innerHTML = `
                <tr>
                    <th>模块ID</th>
                    <th>子模块ID(cmdid)</th>
                    <th>变更时间</th>
                </tr>
            `;
            table.appendChild(thead);
            
            // 表格内容
            const tbody = document.createElement('tbody');
            changes.forEach(change => {
                const tr = document.createElement('tr');
                tr.innerHTML = `
                    <td>${change.modid}</td>
                    <td>${change.cmdid}</td>
                    <td>${new Date(change.time).toLocaleString()}</td>
                `;
                tbody.appendChild(tr);
            });
            table.appendChild(tbody);
            
            changeList.innerHTML = '';
            changeList.appendChild(table);
        }

        // 显示错误消息
        function showError(message) {
            const alert = document.createElement('div');
            alert.className = 'alert alert-danger';
            alert.textContent = message;
            document.querySelector('.container').insertBefore(alert, document.getElementById('changeList'));
            setTimeout(() => alert.remove(), 3000);
        }

        // 页面加载时获取变更记录
        document.addEventListener('DOMContentLoaded', fetchChangeList);
    </script>
</body>
</html>
